/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UrlToImage.java
 *
 * Created on 14/09/2010, 06:45:57 PM
 */
package url.to.image;

import utils.Servers.SubManga;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.UIManager;
import org.jdesktop.swingx.JXImagePanel;

/**
 *
 * @author killan
 */
public class UrlToImage extends javax.swing.JFrame {

    private URL url;
    private SubManga sm;
    private URLConnection urlCon;
    private UtilFiles Uf = new UtilFiles();
    private Image image;
    private String pathMan = "c:/imanga/Mangas/";
    private String ext = "jpg";
    private String globPath = "C:/imanga/";

    /** Creates new form UrlToImage */
    public UrlToImage() {
        super("Url To Image");
        initComponents();
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
        }

        File f = new File( globPath + "Conf/series.konf");
        try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
            while ((linea = br.readLine()) != null) {
                 jComboBox1.addItem(linea);
            }
            br.close();
            f = new File( globPath + "Conf/MangaList/" +  jComboBox1.getItemAt(0) + ".konf");
            br = new BufferedReader(new FileReader(f));
            linea = "";
            while ((linea = br.readLine()) != null) {
                int idx = linea.indexOf("/");
                 jComboBox2.addItem(linea.substring(0, idx));
            }
            br.close();
        } catch (Exception ex) {
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jXImagePanel1 = new org.jdesktop.swingx.JXImagePanel();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jComboBox2 = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField1.setText("http://img.submanga.com:8080/pages/87/87238e5b/1.jpg");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jXImagePanel1.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jXImagePanel1Layout = new javax.swing.GroupLayout(jXImagePanel1);
        jXImagePanel1.setLayout(jXImagePanel1Layout);
        jXImagePanel1Layout.setHorizontalGroup(
            jXImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 186, Short.MAX_VALUE)
        );
        jXImagePanel1Layout.setVerticalGroup(
            jXImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 243, Short.MAX_VALUE)
        );

        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("jLabel3");

        jButton3.setText("jButton3");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jComboBox2.setMaximumSize(new java.awt.Dimension(166, 32767));

        jButton4.setText("jButton4");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("jButton5");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("jButton6");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(102, 102, 102))
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(141, 141, 141))
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5))
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jButton6))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jComboBox1, 0, 190, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jXImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXImagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4)
                            .addComponent(jButton5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6)))
                .addGap(51, 51, 51))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void DDdef() {
        pathMan = "c:/imanga/Mangas/Naruto/";
        try {
            Uf.newDir(new File(pathMan));
        } catch (IOException ex) {
        }
        out:
        for (int i = 1; i < 100; i++) {
            try {
                jLabel1.setText("sin error");
                File FE = new File(pathMan + i + "." + ext);
                if (!FE.exists()) {
                    url = new URL("http://img.submanga.com:8080/pages/87/878785a9/" + i + ".jpg");
                    //url=new URL("http://static3.mangastream.com/manga/3/70259148/"+i+".jpg");
                    urlCon = url.openConnection();
                    // acceso al contenido web

                    InputStream is = urlCon.getInputStream();

                    // Fichero en el que queremos guardar el contenido
                    FileOutputStream fos = new FileOutputStream(pathMan + i + "." + ext);

                    // buffer para ir leyendo.
                    byte[] array = new byte[1024];

                    // Primera lectura y bucle hasta el final
                    int leido = is.read(array);
                    while (leido > 0) {
                        fos.write(array, 0, leido);
                        leido = is.read(array);
                    }

                    // Cierre de conexion y fichero.
                    is.close();
                    fos.close();
                }
            } catch (Exception e) {
                //jLabel1.setText(e.toString());
                break out;
            }
        }
        jLabel2.setText("" + urlCon.getContentType());

        /*
        JFileChooser fc = new JFileChooser();
        int dx = fc.showOpenDialog(this);
        File f2;
        if (dx == fc.APPROVE_OPTION) {
        f2 = fc.getSelectedFile();
        jLabel3.setText(f2.getAbsolutePath());
        }
         * 
         */
        image = getImagen(pathMan + "1." + ext);
        //jLabel2.setText("Imagen cargada");
        setImageinImagePanel(pathMan + "1." + ext, jXImagePanel1);
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        DDdef();
    }//GEN-LAST:event_jButton1ActionPerformed

    public Image getImagen(String url) {
        Image imagen;
        BufferedImage bufferedImage = null;
        //Realizamos la lectura del archivo.
        File archivo = new File(url);
        try {
            bufferedImage = (ImageIO.read(archivo));
        } catch (IOException ex) {
        }
        imagen = bufferedImage;
        return imagen;
    }//fin mètodo: getImagen()..

    /*  public void setImageinImagePanel(Image imagen, JXImagePanel panel) {
    JXImagePanel paneldeImagen = panel;
    paneldeImagen.setImage(null);
    paneldeImagen.setImage(imagen);
    }//fin método: setImageinImagePanel*/
    public void setImageinImagePanel(String url, JXImagePanel panel) {
        //Inicializamos nuestra variable.
        JXImagePanel paneldeImagen = panel;
        Image imagen = null;
        paneldeImagen.setImage(null);
        //Obtenemos la imagen a partir de la url proporcionada.
        imagen = getImagen(url);
        //Obtenemos el ancho y alto del panel el caul no servira para redimen---
        //sionar a la imagen.
        int ancho = panel.getWidth();
        int alto = panel.getHeight();
        imagen = redimensionarImagen(imagen, ancho, alto);
        //Insertamos la imagen redimensionada en el panel.
        paneldeImagen.setImage(imagen);
    }//Fin metodo: setImageinImagePanel.

    public Image redimensionarImagen(Image imagen, int ancho, int alto) {
        return imagen.getScaledInstance(ancho, alto, Image.SCALE_AREA_AVERAGING);
    }//FIN: redimensionarImagen.

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        try {
             Uf.newDir(new File( globPath));
        } catch (IOException ex) {
        }
         sm = new SubManga();
         sm.getSeries( globPath);
        try {
            File f = new File( globPath + "Conf/series.konf");
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
            if ((linea = br.readLine()) != null) {
                 sm.getChaptersList( globPath, linea, "completa");
            }
            br.close();

        } catch (Exception ex) {
        }
         jLabel3.setText("Ok Html");

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        String mang = jComboBox1.getSelectedItem().toString();
        String cap = jComboBox2.getSelectedItem().toString();
        jLabel1.setText(sm.getNumPages(mang, cap) + "");
        sm.getPages(mang, cap);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        String mang =  jComboBox1.getSelectedItem().toString();

        //String cap = jComboBox2.getSelectedItem().toString();
        for (int i =  jComboBox2.getItemCount() - 1; i >= 0; i--) {
             sm.getPages(mang,  jComboBox2.getItemAt(i).toString());
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        String mang =  jComboBox1.getSelectedItem().toString();
        String cap =  jComboBox2.getSelectedItem().toString();
         jLabel1.setText( sm.getNumPages(mang, cap) + "");
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        try {
            File f = new File( globPath + "Conf/MangaList/"
                    +  jComboBox1.getSelectedItem().toString() + ".konf");
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
             jComboBox2.removeAllItems();
            while ((linea = br.readLine()) != null) {
                int idx = linea.indexOf("/");
                 jComboBox2.addItem(linea.substring(0, idx));
            }
            br.close();
        } catch (Exception e) {
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                UrlToImage uTI = new UrlToImage();
                uTI.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private org.jdesktop.swingx.JXImagePanel jXImagePanel1;
    // End of variables declaration//GEN-END:variables
}
